<?php

namespace App\Console\Commands;

use App\Models\MakeMaster;
use App\Models\ModelMaster;
use App\Models\Warranty;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use function Symfony\Component\Translation\t;

class UpdateStoreCode extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:updateStoreCode';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->pushData();
        return 0;
    }

    public function pushData()
    {
        $page = 1;
        $pageSize = 3000;
        while (true) {
            $model = DB::table('users')
                ->leftJoin('store_info', 'users.id', 'store_info.user_id')
                ->leftJoin('cities', 'cities.id', 'users.city')
                ->leftJoin('province_master', 'province_master.id', 'users.province_id');
            $stores = $model
                ->select(
                    'users.company_name',
                    'users.id as user_id',
                    'users.abbr',
                    'users.address',
                    'users.phone_number',
                    'users.first_name',
                    'users.zsdj',
                    'cities.simplified_chiness_value as city_name',
                    'province_master.simplified_chiness_value as province_name',
                    'province_master.id as province_id',
                    'users.unique_id as store_code',
                    'users.status',
                    'users.category_code',
                    'users.category_name',
                    'store_info.new_store_code'
                )
                ->whereNull('users.deleted_at')
                ->where('users.status', 1)
                ->whereIn('users.user_type', [2, 3])
                ->where('users.creator_id', '!=', 299)
                ->offset(($page - 1) * $pageSize)
                ->limit($pageSize)
                ->get();
            dump($stores);
            if (empty($stores) || $stores->isEmpty()) {
                break;
            }

            foreach ($stores as $store) {
                if (!empty($store->new_store_code) || empty($store->province_id)) {
                    continue;
                }
                dump($store);
                $province = DB::table('province_master')->where('id', $store->province_id)->first();
                $sql = 'SELECT COUNT(1) AS cnt FROM store_info WHERE new_store_code LIKE "' . $province->abbreviation . '%"';
                $result = DB::select($sql);
                $newStoreCode = $province->abbreviation . str_pad($result[0]->cnt + 1, 3, 0, STR_PAD_LEFT);
                dump($newStoreCode);
                $storeCode = DB::table('store_info')->where('new_store_code', $newStoreCode)->first();
                if (!empty($storeCode)) {
                    $newStoreCode = $province->abbreviation . str_pad($result[0]->cnt + 2, 3, 0, STR_PAD_LEFT);
                    $storeCode = DB::table('store_info')->where('new_store_code', $newStoreCode)->first();
                    if (!empty($storeCode)) {
                        $newStoreCode = $province->abbreviation . str_pad($result[0]->cnt + 3, 3, 0, STR_PAD_LEFT);
                        $storeCode = DB::table('store_info')->where('new_store_code', $newStoreCode)->first();
                        if (!empty($storeCode)) {
                            $newStoreCode = $province->abbreviation . str_pad($result[0]->cnt + 4, 3, 0, STR_PAD_LEFT);
                        }
                    }
                }

                $user = DB::table('store_info')->where('user_id', $store->user_id)->first();
                if (empty($user)) {
                    $updateStoreResult = DB::table('store_info')->insert([
                        'user_id' => $store->user_id,
                        'new_store_code' => $newStoreCode
                    ]);
                    dump($updateStoreResult);
                } else {
                    DB::table('store_info')->where('id', $user->id)
                        ->update([
                            'new_store_code' => $newStoreCode
                        ]);
                }
            }
            $page++;
        }
    }
}
